<?php
namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class DataExportController extends BaseController
{

    public function index(Request $request)
    {
        try {

            $type = $request->input('type');
            if (empty($type)) {
                return response()->json(['status' => '404', 'data' => '发布数据类型为空']);
            }
            $data = $request->input('data');
            // dd(json_decode(base64_decode($data)));
            // $data = unserialize($data);
            // dd($data,unserialize($data));
            // // dd(json_decode($data));
            // $result = ($data);
            $name = $this->getLogNameByType($type);
            Log::info('測試', [
                // 请求信息
                'request_info' => [
                    // 请求IP
                    'IP'     => $request->getClientIp() ? $_SERVER['REMOTE_ADDR'] : $request->getClientIp(),
                    // 请求方式
                    'method' => $request->getMethod(),
                    // 请求的参数
                    'params' => count($data),
                ],
            ]);

            // try {
            //     $export = new DataExport();
            //     $export->writeJsonData($data, $type);
            // } catch (Exception $e) {

            // }
            return response()->json(['status' => '200', 'data' => '数据发布成功']);
        } catch (Exception $e) {
            dd($e->getMessage());
        }
    }

    /**
     * 根据类型获取日志记录名称
     */
    public function getLogNameByType($type)
    {
        $name = '未知';
        switch ($type) {
            case 'vasseti':
                $name = '发布媒资静态数据';
                break;
            case 'vseriali':
                $name = '发布分集静态数据';
                break;
            case 'column':
                $name = '发布一级栏目静态数据';
                break;
            case 'subcolumn':
                $name = '发布二级栏目静态数据';
                break;
            case 'column_recommend':
                $name = '发布一级栏目推荐位静态数据';
                break;
            case 'subcolumn_recommend':
                $name = '发布二级栏目推荐位静态数据';
                break;
            default:
                $name = '未知';
                break;
        }
        return $name;
    }
}
